<?php
class smartmenu extends htmlwidget {
	public $mainitems = array();
	public $subitems = array();
	
	public function __construct(){
		$this->tag = 'ul';
	}
	
	public function addItem($mainitem, $subitems){
		$this->mainitems[] = $mainitem;
		$this->subitems[] = $subitems;				
	}
	
	public function __toString(){

		
		//opening tag
		$this->html = '<' . $this->tag;

		foreach ( $this->attributes as $key => $value){
			if ( ! empty($value) || strlen($value)){
				$this->html .= ' ' . $key . '="' . $value . '"';
			}
		}
		$this->html .= '>' . "\n";

		//add mainitem element and item(s)		
		while ( count($this->mainitems)){
			
			$mainitem = array_shift($this->mainitems);		
			$subitems = array_shift($this->subitems);

			if ( $subitems instanceof smartmenu ){
				$this->html .= '<li>';
				$this->html .= $mainitem->__toString();
				$this->html .= $subitems->__toString();
				$this->html .= '</li>' . "\n";
			} elseif ( is_null($subitems)){
				$this->html .= '<li>';
				$this->html .= $mainitem->__toString();
				$this->html .= '</li>' . "\n";				
			}
			
			
			
			
		}

		//closing tag
		$this->html .= "\n</" . $this->tag . ">\n";
		return $this->html;
	}
	
}
?>